package com.chery.media.utils

import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.util.Utils
import com.chery.media.library.extend.loge
import com.chery.media.library.extend.toJson

object WifiManagerHelper {

    val wifiManager = Utils.getApp().applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

    val wifiReceiver = object : BroadcastReceiver(){

        override fun onReceive(context: Context?, intent: Intent?) {
            if (ActivityCompat.checkSelfPermission(
                    Utils.getApp(),
                    Manifest.permission.ACCESS_FINE_LOCATION
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return
            }
            Utils.getApp().unregisterReceiver(this)
            wifiManager.scanResults.toJson().loge("2222222222")
        }

    }

    /**
     * wifi 开关状态
     */
    fun wifiSwitchStatus(): Boolean {
        return wifiManager.isWifiEnabled
    }

    /**
     * wifi 开关
     */
    fun wifiSwitch(isOpen: Boolean) {
        wifiManager.isWifiEnabled = isOpen
    }

    /**
     * 扫描wifi
     */
    fun scanWifi() {
        Utils.getApp().registerReceiver(wifiReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        wifiManager.startScan()
    }

    /**
     * 获取已连接的wifi
     */
    fun getConnectedWifi() {
        val connectivityManager = Utils.getApp().applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connectivityManager.activeNetworkInfo
        networkInfo.toJson().loge("33333333333====")
    }

}